/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.ila.client.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.ila.client.api.entity.CmsEvent;
import com.pig4cloud.ila.client.service.CmsEventService;
import com.pig4cloud.pig.common.core.constant.CommonConstants;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 *
 *
 * @author KonBAI
 * @date 2020-11-05 00:38:10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/event" )
@Api(value = "event", tags = "管理")
public class CmsEventController {

    private final CmsEventService cmsEventService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param cmsEvent
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getCmsEventPage(Page page, CmsEvent cmsEvent) {
        String remindTimeColumn = "DATE_FORMAT(remind_time,'%Y-%m-%d') = DATE_FORMAT({0},'%Y-%m-%d')";
        return R.ok(cmsEventService.page(page, Wrappers.lambdaQuery(new CmsEvent())
                .eq(CmsEvent::getDelFlag, CommonConstants.STATUS_NORMAL)
                .eq(CmsEvent::getCreateBy, SecurityUtils.getMemberId())
                .apply(ObjectUtil.isNotEmpty(cmsEvent.getRemindTime()), remindTimeColumn, cmsEvent.getRemindTime())
                .orderByDesc(CmsEvent::getCreateTime)));
    }


    /**
     * 通过id查询
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) String id) {
        return R.ok(cmsEventService.getById(id));
    }

    /**
     * 通过月份查询
     * @param date
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/getByDate")
    public R getByDate(String date) {
        return R.ok(cmsEventService.getByDate(date));
    }

    /**
     * 新增
     * @param cmsEvent
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增" )
    @PostMapping
    public R save(@RequestBody CmsEvent cmsEvent) {
        return R.ok(cmsEventService.save(cmsEvent));
    }

    /**
     * 修改
     * @param cmsEvent
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改" )
    @PutMapping
    public R updateById(@RequestBody CmsEvent cmsEvent) {
        return R.ok(cmsEventService.updateById(cmsEvent));
    }

    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除" )
    @DeleteMapping("/{id}" )
    public R removeById(@PathVariable String id) {
        return R.ok(cmsEventService.removeById(id));
    }

}
